package net.mysoft.budgetsoft.system.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import net.mysoft.budgetsoft.system.dao.XiTongXXDao;
import net.mysoft.budgetsoft.system.dao.YongHuDao;
import net.mysoft.budgetsoft.system.domain.TXiTongXX;
import net.mysoft.budgetsoft.system.domain.TYongHu;
import net.mysoft.budgetsoft.system.pagemodel.XiTongXX;
import net.mysoft.budgetsoft.system.service.XiTongXXService;
import net.mysoft.framework.model.DataGrid;
import net.mysoft.framework.model.SessionInfo;
import net.mysoft.framework.util.ResourceUtil;

import org.apache.struts2.ServletActionContext;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

/**
 * 
 * @author dagongPGP v1.0
 * @link <a href=http://www.dagongsoft.net>北京大公信软科技有限公司</a>
 * 
 * @version $Revision: 1.00 $Date: 2014-01-11 18:03:54
 */
@Service("xiTongXXService")
public class XiTongXXServiceImpl implements XiTongXXService {
	@Resource
	private XiTongXXDao xiTongXXDao;
	@Resource
	private YongHuDao yongHuDao;

	public XiTongXXDao getXiTongXXDao() {
		return this.xiTongXXDao;
	}

	public void setXiTongXXDao(XiTongXXDao xiTongXXDao) {
		this.xiTongXXDao = xiTongXXDao;
	}

	@Override
	public DataGrid datagrid(XiTongXX xiTongXX) {
		xiTongXX.setOrder("desc");
		xiTongXX.setSort("t.shiJian");

		DataGrid dg = new DataGrid();
		String hql = "from TXiTongXX t where 1=1 ";
		List params = new ArrayList();
		hql = addWhere(xiTongXX, hql, params);
		String totalHql = "select count(*) " + hql;
		hql = addOrder(xiTongXX, hql);
		List<TXiTongXX> toList = xiTongXXDao.find(hql, params, xiTongXX.getPage(), xiTongXX.getRows());
		List<XiTongXX> oList = new ArrayList<XiTongXX>();
		oList = changeModel(toList);
		dg.setTotal(xiTongXXDao.count(totalHql, params));
		dg.setRows(oList);
		return dg;
	}

	private String addWhere(XiTongXX xiTongXX, String hql, List<Object> params) {
		if (xiTongXX.getZhuangTai() != null) {
			hql += "and t.zhuangTai = ?";
			params.add(xiTongXX.getZhuangTai());
		}
		if (xiTongXX.getToId() != null && !"".equals(xiTongXX.getToId())) {
			hql += "and t.toId = ?";
			params.add(xiTongXX.getToId());
		}
		if (xiTongXX.getFromYongHuM() != null && !"".equals(xiTongXX.getFromYongHuM())) {
			hql += "and t.fromId in (select tt.bianHao from TYongHu tt where tt.xingMing like ?)";
			params.add("%" + xiTongXX.getFromYongHuM() + "%");
		}
		if (xiTongXX.getDatetimeStart() != null) {
			hql += "and t.shiJian >= ?";
			params.add(xiTongXX.getDatetimeStart());
		}
		if (xiTongXX.getDatetimeEnd() != null) {
			hql += "and t.shiJian <= ?";
			params.add(xiTongXX.getDatetimeEnd());
		}
		return hql;
	}

	private String addOrder(XiTongXX xiTongXX, String hql) {
		if (xiTongXX.getSort() != null) {
			hql += " order by " + xiTongXX.getSort() + " " + xiTongXX.getOrder();
		}
		return hql;
	}

	public List<XiTongXX> changeModel(List<TXiTongXX> tXiTongXX) {
		List<XiTongXX> list = new ArrayList<XiTongXX>();
		for (TXiTongXX to : tXiTongXX) {
			XiTongXX o = new XiTongXX();
			BeanUtils.copyProperties(to, o);

			if (to.getFromId() != null && !"".equals(to.getFromId())) {
				TYongHu yongHu = (TYongHu) yongHuDao.get(TYongHu.class, to.getFromId());
				o.setFromYongHuM(yongHu.getXingMing());
			}

			if (to.getToId() != null && !"".equals(to.getToId())) {
				TYongHu yongHu = (TYongHu) yongHuDao.get(TYongHu.class, to.getToId());
				o.setToYongHuM(yongHu.getXingMing());
			}

			list.add(o);
		}
		return list;
	}

	/**
	 * 邮件删除（假删除）
	 */
	@Override
	public void del(XiTongXX xiTongXX) {
		for (String id : xiTongXX.getIds().split(",")) {
			TXiTongXX t = (TXiTongXX) xiTongXXDao.get(TXiTongXX.class, id);
			if (t != null) {
				t.setZhuangTai(TXiTongXX.SHANCHU);
				xiTongXXDao.update(t);
			}
		}
	}

	/**
	 * 标记为已阅
	 */
	@Override
	public void seen(XiTongXX xiTongXX) {
		for (String id : xiTongXX.getIds().split(",")) {
			TXiTongXX t = (TXiTongXX) xiTongXXDao.get(TXiTongXX.class, id);
			if (t != null) {
				t.setZhuangTai(TXiTongXX.YIYUE);
				xiTongXXDao.update(t);
			}
		}
	}

	/**
	 * 统计未读消息的数目
	 */
	@Override
	public int msgCount(XiTongXX xiTongXX) {
		long msgCount = 0;
		String hql = "select count(*) from TXiTongXX t where t.toId=? and t.zhuangTai=?";
		List params = new ArrayList();
		SessionInfo sessionInfo = (SessionInfo) ServletActionContext.getRequest().getSession().getAttribute(ResourceUtil.getSessionInfoName());
		params.add(sessionInfo.getUserId());
		params.add(TXiTongXX.WEIYUE);
		msgCount = xiTongXXDao.count(hql, params);
		return (int) msgCount;
	}

	/**
	 * 发消息 允许有多个收件人id，需要用逗号分隔
	 */
	/*
	@Override
	public void sendMsg(String faJianRID, String shouJianRID, String content, Integer xiaoXiLX, Integer shiFouTC) {
		for (String shouJianR : shouJianRID.split(",")) {
			TXiTongXX xx = new TXiTongXX();
			xx.setBianHao(UUID.randomUUID().toString());
			xx.setFromId(faJianRID);
			xx.setToId(shouJianR);
			xx.setNeiRong(content);
			xx.setShiJian(new Date());
			xx.setZhuangTai(TXiTongXX.WEIYUE);
			xx.setCuiBanLX(xiaoXiLX);
			xx.setShiFouTC(shiFouTC);
			xiTongXXDao.save(xx);
		}
	}*/

	/**
	 * 获取催办信息
	 */
	@Override
	public List<XiTongXX> huoQuCBXX(XiTongXX xiTongXX) {
		String hql = "from TXiTongXX t where t.toId=? and t.cuiBanLX=? and t.shiFouTC=?";
		List params = new ArrayList();
		SessionInfo sessionInfo = (SessionInfo) ServletActionContext.getRequest().getSession().getAttribute(ResourceUtil.getSessionInfoName());
		params.add(sessionInfo.getUserId());
		params.add(TXiTongXX.CUIBAN);
		params.add(TXiTongXX.WEITANCHUANG);
		List<TXiTongXX> toList = xiTongXXDao.find(hql, params, xiTongXX.getPage(), xiTongXX.getRows());
		List<XiTongXX> oList = new ArrayList<XiTongXX>();
		oList = changeModel(toList);

		// 设置成已弹窗状态
		for (TXiTongXX t : toList) {
			t.setShiFouTC(TXiTongXX.YITANCHUANG);
			xiTongXXDao.update(t);
		}
		return oList;
	}
}
